package cn.iocoder.yudao.module.cmt.controller.admin.activity;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.activity.ActivitySignupDO;
import cn.iocoder.yudao.module.cmt.service.activity.ActivitySignupService;

@Tag(name = "管理后台 - 活动报名")
@RestController
@RequestMapping("/cmt/activity-signup")
@Validated
public class ActivitySignupController {

    @Resource
    private ActivitySignupService activitySignupService;

    @PostMapping("/create")
    @Operation(summary = "创建活动报名")
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:create')")
    public CommonResult<Long> createActivitySignup(@Valid @RequestBody ActivitySignupSaveReqVO createReqVO) {
        return success(activitySignupService.createActivitySignup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动报名")
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:update')")
    public CommonResult<Boolean> updateActivitySignup(@Valid @RequestBody ActivitySignupSaveReqVO updateReqVO) {
        activitySignupService.updateActivitySignup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动报名")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:delete')")
    public CommonResult<Boolean> deleteActivitySignup(@RequestParam("id") Long id) {
        activitySignupService.deleteActivitySignup(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除活动报名")
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:delete')")
    public CommonResult<Boolean> deleteActivitySignupList(@RequestParam("ids") List<Long> ids) {
        activitySignupService.deleteActivitySignupListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动报名")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:query')")
    public CommonResult<ActivitySignupRespVO> getActivitySignup(@RequestParam("id") Long id) {
        ActivitySignupDO activitySignup = activitySignupService.getActivitySignup(id);
        return success(BeanUtils.toBean(activitySignup, ActivitySignupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动报名分页")
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:query')")
    public CommonResult<PageResult<ActivitySignupRespVO>> getActivitySignupPage(@Valid ActivitySignupPageReqVO pageReqVO) {
        PageResult<ActivitySignupDO> pageResult = activitySignupService.getActivitySignupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivitySignupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动报名 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:activity-signup:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportActivitySignupExcel(@Valid ActivitySignupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivitySignupDO> list = activitySignupService.getActivitySignupPage(pageReqVO).getList();
        ExcelUtils.write(response, "活动报名.xls", "数据", ActivitySignupRespVO.class,
                        BeanUtils.toBean(list, ActivitySignupRespVO.class));
    }

}
